package com.yjhb.android.sbz.v2.activity

import android.graphics.Color
import com.jojo.android.zxlib.tool.ZxDataTool
import com.jojo.android.zxlib.tool.ZxSpanTool
import com.jojo.android.zxlib.tool.ZxToast
import com.yjhb.android.sbz.R
import com.yjhb.android.sbz.activity.BaseActivity
import com.yjhb.android.sbz.imps.BaseTextChangedListener
import com.yjhb.android.sbz.tool.EventMsg
import com.yjhb.android.sbz.tool.Tool
import com.yjhb.android.sbz.v2.mvp.presenter.BindPresenter
import com.yjhb.android.sbz.v2.mvp.view.V2BindView
import kotlinx.android.synthetic.main.activity_real_name.*
import org.greenrobot.eventbus.EventBus


/**
 *  创建者 zx
 *  创建时间 2021/8/10 16:14
 */
class V2BindRealNameActivity : BaseActivity<BindPresenter>(), V2BindView {


    override fun initPresenter() = BindPresenter(this)

    override fun getLayoutId() = R.layout.activity_real_name

    override fun initUI() {

    }

    override fun initData() {

    }

    override fun regEvent() {
        tips_tv?.text = ZxSpanTool.getHighlightSpan("根据国家部门规定,需要对提现收款账户进行实名认证,实名认证通过后，账户才能开启提现。\n" +
                "1、支付宝和微信的提现收款账户，必须和实名信息一致。(如果实名信息不一致，提现将失败)\n" +
                "2、请认真填写，实名认证绑定成功后，将不允许修改。", Color.parseColor("#FF584B"),
                "实名认证通过后，账户才能开启提现")
        back?.setOnClickListener {
            finish()
        }
        confirm_tv?.setOnClickListener {
            if (Tool.isEditEmpty(et_name)) {
                ZxToast.showBottomToastLight("请输入姓名！")
                return@setOnClickListener
            }
            if (Tool.isEditEmpty(et_idCard)) {
                ZxToast.showBottomToastLight("请输入身份证号！")
                return@setOnClickListener
            }
            if (!ZxDataTool.isIdCard(et_idCard.text.toString())) {
                ZxToast.showBottomToastLight("身份证号有误，请查正后再输入！")
                return@setOnClickListener
            }
            presenter?.updateIdentityInfo(et_name.text.toString(), et_idCard.text.toString())
        }
        et_name?.addTextChangedListener(object : BaseTextChangedListener() {
            override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) {
                if (!ZxDataTool.isChineseWord(s.toString())) {
                    et_name?.let {
                        //移除监听
                        it.removeTextChangedListener(this)
                        // 设置处理完的s
                        it.setText(clearLimitStr(s.toString()))
                        it.setSelection(it.text.length)
                        // 重新添加监听
                        it.addTextChangedListener(this)
                    }

                }
            }
        })
        et_idCard?.addTextChangedListener(object : BaseTextChangedListener() {
            override fun onTextChanged(s: CharSequence, start: Int, before: Int, count: Int) {
                et_idCard?.let {
                    //移除监听
                    it.removeTextChangedListener(this)
                    // 设置处理完的s
                    it.setText(s.toString().replace("[^[0123456789xX]]".toRegex(), ""))
                    it.setSelection(it.text.length)
                    // 重新添加监听
                    it.addTextChangedListener(this)
                }

            }
        })

    }

    /**
     * 清除不是中文的内容
     *
     * @return
     */
    private fun clearLimitStr(str: String): String? {
        return str.replace("[^[\u4E00-\u9FBF],\u00B7]".toRegex(), "")
    }

    override fun onFailure(code: Int, msg: String?) {
        ZxToast.showBottomToastLight(msg)
        loadingDialog?.cancel()
    }

    override fun onWait(msg: String) {
        loadingDialog?.setTipsText(msg)
        loadingDialog?.show()
    }

    override fun onBindSuccess(type: Int) {
        loadingDialog?.cancel()
        ZxToast.showBottomToastLight("实名信息绑定成功")
        EventBus.getDefault().post(EventMsg(EventMsg.REQUEST_USER_UPDATE, null))
        finish()
    }
}